<template>
  <div class="cart-container">
    <div class="cart-header-section">
      <h1>我的购物车</h1>
      <p class="cart-summary">共 {{ cartItems.length }} 件商品</p>
    </div>

    <el-card class="cart-content" :body-style="{ padding: '0px' }">
      <template #header>
        <div class="cart-toolbar">
          <div class="selection-info">
            <el-checkbox 
              v-model="allSelected" 
              @change="handleSelectAll"
            >全选</el-checkbox>
            <span class="selected-count" v-if="cartItems.length">
              已选择 {{ selectedCount }} 件商品
            </span>
          </div>
          <el-button 
            type="danger" 
            plain
            size="small" 
            @click="clearCart"
            v-if="cartItems.length"
          >
            <el-icon><Delete /></el-icon>
            清空购物车
          </el-button>
        </div>
      </template>

      <div v-if="cartItems.length === 0" class="empty-cart">
        <el-empty description="购物车还是空的">
          <template #image>
            <img src="https://via.placeholder.com/200" alt="空购物车" class="empty-cart-image">
          </template>
          <template #description>
            <p>快去添加一些美味的零食吧！</p>
          </template>
          <el-button type="primary" @click="$router.push('/')">
            去购物
          </el-button>
        </el-empty>
      </div>

      <div v-else class="cart-list">
        <div v-for="item in cartItems" :key="item.id" class="cart-item">
          <div class="item-main">
            <el-checkbox v-model="item.selected" @change="updateSelection"/>
            <img :src="item.image" class="item-image" :alt="item.name">
            <div class="item-info">
              <h3 class="item-name">{{ item.name }}</h3>
              <p class="item-brand">{{ item.brand }}</p>
              <div class="item-tags" v-if="item.tags">
                <el-tag size="small" v-for="tag in item.tags" :key="tag" type="info">
                  {{ tag }}
                </el-tag>
              </div>
            </div>
          </div>

          <div class="item-price">
            <span class="price-label">单价</span>
            <span class="price-value">¥{{ item.price.toFixed(2) }}</span>
          </div>

          <div class="item-quantity">
            <span class="quantity-label">数量</span>
            <el-input-number 
              v-model="item.quantity" 
              :min="1" 
              :max="item.stock"
              size="small"
              @change="updateQuantity(item.id, $event)"
            />
          </div>

          <div class="item-subtotal">
            <span class="subtotal-label">小计</span>
            <span class="subtotal-value">¥{{ (item.price * item.quantity).toFixed(2) }}</span>
          </div>

          <div class="item-actions">
            <el-button 
              type="danger" 
              circle
              size="small"
              @click="removeFromCart(item.id)"
            >
              <el-icon><Delete /></el-icon>
            </el-button>
          </div>
        </div>
      </div>

      <div class="cart-footer" v-if="cartItems.length">
        <div class="footer-left">
          <el-checkbox v-model="allSelected" @change="handleSelectAll">全选</el-checkbox>
          <el-button type="text" @click="clearSelected">删除选中商品</el-button>
        </div>
        <div class="footer-right">
          <div class="total-info">
            <p class="selected-items">已选择 {{ selectedCount }} 件商品</p>
            <p class="total-price">
              总计: <span class="price">¥{{ cartTotal.toFixed(2) }}</span>
            </p>
          </div>
          <el-button type="primary" size="large" @click="checkout" :disabled="!selectedCount">
            结算 ( {{ selectedCount }} )
          </el-button>
        </div>
      </div>
    </el-card>

    <!-- 推荐商品 -->
    <div class="recommendations" v-if="cartItems.length">
      <h2>猜你喜欢</h2>
      <el-row :gutter="20">
        <el-col :span="6" v-for="item in recommendItems" :key="item.id">
          <el-card :body-style="{ padding: '0px' }" class="recommend-card">
            <img :src="item.image" class="recommend-image">
            <div class="recommend-info">
              <h4>{{ item.name }}</h4>
              <p class="recommend-price">¥{{ item.price.toFixed(2) }}</p>
              <el-button type="primary" size="small" @click="addToCart(item)">
                加入购物车
              </el-button>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useStore } from 'vuex'
import { ElMessage, ElMessageBox } from 'element-plus'

export default {
  name: 'CartPage',
  setup() {
    const store = useStore()
    const allSelected = ref(false)

    const cartItems = computed(() => store.state.cart.items)
    const selectedCount = computed(() => 
      cartItems.value.filter(item => item.selected).length
    )
    const cartTotal = computed(() => 
      cartItems.value
        .filter(item => item.selected)
        .reduce((total, item) => total + (item.price * item.quantity), 0)
    )

    // 模拟推荐商品数据
    const recommendItems = ref([
      // 添加推荐商品数据
    ])

    // 方法实现...
    
    return {
      cartItems,
      allSelected,
      selectedCount,
      cartTotal,
      recommendItems,
      // ...其他方法
    }
  }
}
</script>

<style scoped>
.cart-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.cart-header-section {
  margin-bottom: 20px;
}

.cart-header-section h1 {
  font-size: 28px;
  font-weight: 500;
  margin: 0;
  color: #333;
}

.cart-summary {
  color: #666;
  margin-top: 8px;
}

.cart-content {
  background: #fff;
  border-radius: 8px;
  margin-bottom: 30px;
}

.cart-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.selection-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.selected-count {
  color: #666;
  font-size: 14px;
}

.cart-list {
  padding: 0 20px;
}

.cart-item {
  display: flex;
  align-items: center;
  padding: 20px 0;
  border-bottom: 1px solid #f0f0f0;
}

.item-main {
  display: flex;
  align-items: center;
  flex: 3;
  gap: 20px;
}

.item-image {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border-radius: 8px;
}

.item-info {
  flex: 1;
}

.item-name {
  margin: 0 0 8px;
  font-size: 16px;
  color: #333;
}

.item-brand {
  color: #999;
  margin: 0 0 8px;
}

.item-tags {
  display: flex;
  gap: 8px;
}

.item-price,
.item-quantity,
.item-subtotal {
  flex: 1;
  text-align: center;
}

.price-label,
.quantity-label,
.subtotal-label {
  display: block;
  color: #999;
  font-size: 12px;
  margin-bottom: 8px;
}

.price-value,
.subtotal-value {
  color: #ff6b6b;
  font-size: 16px;
  font-weight: 500;
}

.item-actions {
  flex: 0.5;
  text-align: right;
}

.cart-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background: #f8f9fa;
  border-top: 1px solid #eee;
}

.footer-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.footer-right {
  display: flex;
  align-items: center;
  gap: 30px;
}

.total-info {
  text-align: right;
}

.total-price {
  margin: 5px 0 0;
  font-size: 16px;
}

.price {
  color: #ff6b6b;
  font-size: 24px;
  font-weight: bold;
}

.recommendations {
  margin-top: 40px;
}

.recommendations h2 {
  font-size: 20px;
  margin-bottom: 20px;
  color: #333;
}

.recommend-card {
  transition: all 0.3s;
}

.recommend-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.recommend-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.recommend-info {
  padding: 15px;
}

.recommend-info h4 {
  margin: 0 0 10px;
  font-size: 14px;
}

.recommend-price {
  color: #ff6b6b;
  font-size: 16px;
  font-weight: 500;
  margin: 0 0 10px;
}

.empty-cart {
  padding: 60px 0;
  text-align: center;
}

.empty-cart-image {
  width: 200px;
  height: 200px;
  object-fit: contain;
}

@media (max-width: 768px) {
  .cart-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }

  .item-price,
  .item-quantity,
  .item-subtotal {
    text-align: left;
  }

  .cart-footer {
    flex-direction: column;
    gap: 20px;
  }

  .footer-right {
    width: 100%;
    justify-content: space-between;
  }
}
</style>